/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk> and Matthias Buch-Kromann <mbk.isv@cbs.dk>
 *
 *  This file is part of the IncrementalParser package.
 *
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.incparser.parsers;

import java.util.List;
import org.osdtsystem.incparser.featureextraction.FeatureExtractor;
import org.osdtsystem.incparser.featureextraction.MstFeatureExtractorUnlabelled;
import org.osdtsystem.incparser.features.FeatureAggregatorScore;
import org.osdtsystem.incparser.features.FeatureHandler;
import org.osdtsystem.incparser.features.WeightVector;
import org.osdtsystem.incparser.graphs.BaseGraph;
import org.osdtsystem.incparser.graphs.ConllAdapter;
import org.osdtsystem.incparser.mst.ChuLiuEdmonds;

/**
 * 
 * @author Martin Haulrich and Matthias Buch-Kromann
 */
public class MstParserUnlabelled extends AbstractMstParser {
    public MstParserUnlabelled(FeatureHandler edgeTypeHandler,
            FeatureHandler stringHandler, FeatureHandler featureHandler) {
        super(edgeTypeHandler, stringHandler, featureHandler);
    }

    @Override
    FeatureExtractor[][] featureExtractorGroups(ConllAdapter graph) {
        FeatureExtractor[] unlabelledMstExtractorGroup = { unlabelledMstExtractor(graph) };
        FeatureExtractor[][] extractorGroups = {unlabelledMstExtractorGroup};
        return extractorGroups;
    }

    @Override
    public ConllAdapter parse(WeightVector weights, ConllAdapter graph) {
        // Remove edges from graph
        graph.clearEdges();
        graph.clearGold();

        BaseGraph baseGraph = graph.baseGraph();
        int nodes = baseGraph.nodes();

        // Create MST extractors and score aggregator for computing scores
        MstFeatureExtractorUnlabelled unlabelledExtractor = unlabelledMstExtractor(graph);
        FeatureAggregatorScore aggregator = new FeatureAggregatorScore(weights, featureHandler, 0);

        // Compute edge-factored scores and best label for each dependency
        double[][] scores = new double[nodes][nodes];
        for (int head = 0; head < nodes; head++) {
            for (int node = 0; node < nodes; node++) {
                // Find unlabelled score
                aggregator.clear();
                unlabelledExtractor.extractDependency(aggregator, baseGraph, node, head);
                scores[head][node] = aggregator.score();
            }
        }


        // Parse sentence and add edges to graph
        List<Integer> CLEheads = ChuLiuEdmonds.chuLiuEdmonds(scores);
        for (int node = 1; node < nodes; ++node) {
            graph.addEdge(node, CLEheads.get(node), 0);
        }

        // Return parse
        return graph;
    }
}
